package org.yookiee.support.core;

import lombok.extern.slf4j.Slf4j;
import org.yookiee.support.constants.I18nConstant;
import org.yookiee.support.toolkits.DefaultConfigToolkit;

/**
 * FileName I18NContext
 * Package org.yookiee.support.core
 * Description
 * author cpstory
 * create 2023-09-27 11:21
 * version V1.0
 */
@Slf4j
public class I18NContext {

    private static final ThreadLocal<EasyLocale> LOCALE_HOLDER = new ThreadLocal<>();

    public static void add(EasyLocale locale){
        LOCALE_HOLDER.set(locale);
    }


    public static EasyLocale get(){
        EasyLocale locale = LOCALE_HOLDER.get();
        if(locale == null){
            return new EasyLocale(DefaultConfigToolkit.defaultConfig.getLanguage(), DefaultTimeZone.defaultTimeZoneCode(),"");
        }

        //如果lang中包含"-"全部替换为"_",如果以en开头的，全部替换为en
        String contextLang = locale.getLang();
        //处理兼容
        try{
            if(contextLang.startsWith("en")){
                locale.setLang("en");
            }else{
                if(contextLang.contains("-")){
                    locale.setLang(contextLang.replaceAll("-","_"));
                }
            }
        }catch (Exception e){
            log.error("转换国际化语言失败！,{}",e.getMessage());
        }
        return  locale;
    }

    public static void remove(){
        LOCALE_HOLDER.remove();
    }



}
